﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class Sys_DictionaryS: Sys_DictionarySI
    {
        private readonly WmssystemContext myDbContext;

        public Sys_DictionaryS(WmssystemContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        //添加
        public Task<int> Add(Sys_Dictionary dicAdd)
        {
            myDbContext.Sys_Dictionary.Add(dicAdd);
            return myDbContext.SaveChangesAsync();
        }
        //分页模糊查询
        public Task<List<Sys_Dictionary>> FenYe(int pageSize, int currentPage, string Name = null, string Type = null, bool? Status = null)
        {
            var query = myDbContext.Sys_Dictionary.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Name))
            {
                query = query.Where(c => c.D_DictionaryName.Contains(Name));
            }
            if (!string.IsNullOrEmpty(Type))
            {
                query = query.Where(c => c.D_DictionaryType.Contains(Type));
            }
            if (Status.HasValue)
            {
                query = query.Where(c => c.D_Status == Status);
            }
            query = query.Where(c => c.D_IsDelete == true);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //修改
        public Task<int> Upd(Sys_Dictionary dicUpd)
        {
            myDbContext.Sys_Dictionary.Update(dicUpd);
            return myDbContext.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDbContext.Sys_Dictionary.FindAsync(id);
            // 更新实体的删除状态
            entity.D_IsDelete = isDeleted;
            // 标记实体为已修改状态
            myDbContext.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDbContext.SaveChangesAsync();
        }
    }
}
